package com.company.client;

import com.company.common.Message;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.time.LocalDateTime;

public class MessageService {

    public static void sendToOne(String content, String receiver, String sender) {
        Message<String> message = new Message<>(content, Message.MessageType.NORMAL_MSG, receiver, sender, LocalDateTime.now());
        try {
            Socket socket = ConnSvrThdManage.get(sender).getSocket();
            new ObjectOutputStream(socket.getOutputStream()).writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(sender + "对" + receiver + "说：" + content);
    }

    public static void sendToMany(String content, String sender) {
        Message<String> message = new Message<>(content, Message.MessageType.GROUP_MSG, "", sender, LocalDateTime.now());
        try {
            Socket socket = ConnSvrThdManage.get(sender).getSocket();
            new ObjectOutputStream(socket.getOutputStream()).writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(sender + "对大家说：" + content);
    }

    public static void getOfflineMsg(String username) {
        Message<String> message = new Message<>("", Message.MessageType.GET_OFFLINE_MSG, "", username, LocalDateTime.now());
        try {
            Socket socket = ConnSvrThdManage.get(username).getSocket();
            new ObjectOutputStream(socket.getOutputStream()).writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(username + "向服务器获取离线消息/文件...");
    }


}
